package com.gxa.controller;

import com.gxa.common.Constants;
import com.gxa.common.PaginationResult;
import com.gxa.common.ResponseData;
import com.gxa.pojo.Codrule;
import com.gxa.service.EncodSetService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Created with IntelliJ IDEA.
 * User: tx
 * Date: 2020/10/28 11:34
 * Description:编码设置
 * Version: V1.0
 */
@RestController
@RequestMapping("storesreturn")
@Api(value = "增加编码规则",tags = "系统设置")
public class EncodSetController {

    @Autowired
    private EncodSetService encodSetService;

    @Autowired
    private ResponseData responseData;

    @PostMapping("/addEncodSet")
    @ApiOperation(value = "新建编码设置信息", notes = "根据信息新建编码设置接口", httpMethod = "post", response = ResponseData.class)
    @ApiResponses({
            @ApiResponse(code = 200, message = "创建成功"),
            @ApiResponse(code = 400, message = "找不到页面"),
            @ApiResponse(code = 500, message = "添加失败")
    })
    public ResponseData addEncodSet(@RequestBody @ApiParam(name = "codrule",value = "编码规则",required = true) Codrule codrule){
        int result = encodSetService.addEncodSet(codrule);
        if(result > 0){
            responseData.setCode(Constants.SUCCESS);
            responseData.setMsg("添加成功");
            responseData.setData(codrule);
        }
        return responseData;
    }



    @PostMapping("/delEncodSet")
    @ApiOperation(value = "删除编码设置信息", notes = "根据信息删除编码设置信息接口", httpMethod = "post", response = ResponseData.class)
    @ApiResponses({
            @ApiResponse(code = 200, message = "删除成功"),
            @ApiResponse(code = 400, message = "找不到页面"),
            @ApiResponse(code = 500, message = "删除失败")
    })
    public ResponseData delEncodSet(@RequestBody @ApiParam(name = "codrule_id",value = "编码id",required = true) Codrule codrule){
        int result = encodSetService.delEncodSet(codrule.getCodruleId());
        if(result > 0){
            responseData.setCode(Constants.SUCCESS);
            responseData.setMsg("删除成功");
            responseData.setData(codrule.getCodruleId());
        }
        return responseData;
    }

    @PostMapping("/updateEncodSet")
    @ApiOperation(value = "更新编码设置信息", notes = "根据信息更新编码设置信息接口", httpMethod = "post", response = ResponseData.class)
    @ApiResponses({
            @ApiResponse(code = 200, message = "更新成功"),
            @ApiResponse(code = 400, message = "找不到页面"),
            @ApiResponse(code = 500, message = "更新失败")
    })
    public ResponseData updateEncodSet(@RequestBody @ApiParam(name = "codrule_id",value = "编码id",required = true) Codrule codrule){
        int result = encodSetService.updateEncodSet(codrule);
        if(result > 0){
            responseData.setCode(Constants.SUCCESS);
            responseData.setMsg("更新成功");
            responseData.setData(codrule.getCodruleId());
        }
        return responseData;
    }

    @GetMapping("/queryEncodSet")
    @ApiOperation(value = "查询编码设置信息", notes = "根据信息查询编码设置信息接口", httpMethod = "get", response = ResponseData.class)
    @ApiResponses({
            @ApiResponse(code = 200, message = "查询成功"),
            @ApiResponse(code = 400, message = "找不到页面"),
            @ApiResponse(code = 500, message = "查询失败")
    })
    public ResponseData queryEncodSet(@RequestParam(defaultValue = "1") int currentPage, @RequestParam(defaultValue = "3")int pageSize){
        PaginationResult paginationResult = encodSetService.queryEncodSet(currentPage,pageSize);
        if(paginationResult != null){
            responseData.setCode(Constants.SUCCESS);
            responseData.setMsg("查询成功");
            responseData.setData(paginationResult);
        }
        return responseData;
    }
}
